﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Practices.Prism.ViewModel;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Win32;
using Mobwiz.SpellChecker.Core;
using Mobwiz.SpellChecker.Core.Phonets;
using Mobwiz.SpellChecker.Core.Dictionaries;
using System.Threading.Tasks;
using System.Windows;

namespace Mobwiz.SpellChecker.DictMaker
{
    public class MainWindowModel : NotificationObject
    {
        private bool busy = false;

        public string PhonetFilePath { get; set; }
        public string WordListFilePath { get; set; }
        public string OutputFilePath { get; set; }

        private bool isSpellPhonet;
        public bool IsAspellPhonet
        {
            get { return isSpellPhonet; }
            set
            {
                isSpellPhonet = value;
                RaisePropertyChanged(() => IsAspellPhonet);
            }
        }


        public ICommand SelectPhonetCommand { get; private set; }
        public ICommand SelectWordListCommand { get; private set; }
        public ICommand SelectOutputCommand { get; private set; }
        public ICommand StartConvertCommand { get; private set; }        

        public int ConvertProgress { get; set; }

        public MainWindowModel()
        {
            this.IsAspellPhonet = true;

            this.SelectPhonetCommand = new DelegateCommand(
                () =>
                {
                    this.PhonetFilePath = SelectFilePath();
                    RaisePropertyChanged(() => this.PhonetFilePath);
                    UpdateCommand(this.StartConvertCommand);
                });

            this.SelectWordListCommand = new DelegateCommand(
                () =>
                {
                    this.WordListFilePath = SelectFilePath();
                    RaisePropertyChanged(() => this.WordListFilePath);
                    UpdateCommand(this.StartConvertCommand);
                });

            this.SelectOutputCommand = new DelegateCommand(
                () =>
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    var result = sfd.ShowDialog();
                    if (result.HasValue && result.Value)
                    {
                        this.OutputFilePath = sfd.FileName;
                        RaisePropertyChanged(() => OutputFilePath);
                        UpdateCommand(this.StartConvertCommand);
                    }
                });

            this.StartConvertCommand = new DelegateCommand(
                () =>
                {
                    Task task = new Task(() =>
                        {
                            IPhonet phonet;
                            if (IsAspellPhonet)
                            {
                                phonet = new AspellPhonet(this.PhonetFilePath);
                            }
                            else
                            {
                                phonet = new DoubleMetaPhonet();
                            }

                            GenericDictionaryGenerator generator = new GenericDictionaryGenerator(phonet);

                            generator.ConvertProgressChanged += (o, e) =>
                                {
                                    this.ConvertProgress = e.Current * 100 / e.Total;
                                    RaisePropertyChanged(() => ConvertProgress);
                                };

                            generator.StartConvert(this.WordListFilePath);

                            ISpellDictionary dict = new MemoryDictionary();
                            dict.LoadDictionary(generator.DictionarySet);
                            dict.SaveDictionary(this.OutputFilePath);

                            generator.Dispose();
                            dict.Dispose();                      

                            busy = false;
                            UpdateCommand(this.StartConvertCommand);
                            this.ConvertProgress = 0;
                            RaisePropertyChanged(() => ConvertProgress);

                            MessageBox.Show("Done");
                        });

                    busy = true;
                    task.Start();
                    UpdateCommand(this.StartConvertCommand);
                },

                () => (!string.IsNullOrEmpty(PhonetFilePath) || !IsAspellPhonet)
                    && !string.IsNullOrEmpty(WordListFilePath)
                    && !string.IsNullOrEmpty(OutputFilePath)
                    && !busy);

        }

        private string SelectFilePath()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            var result = dlg.ShowDialog();
            if (result.HasValue && result.Value)
            {
                return dlg.FileName;
            }
            else
            {
                return string.Empty;
            }
        }

        private void UpdateCommand(ICommand cmd)
        {
            DelegateCommand dcmd = cmd as DelegateCommand;
            if (dcmd != null)
            {
                dcmd.RaiseCanExecuteChanged();
            }
        }
    }
}
